package com.hollycrm.unicom.sheet.business.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.hollycrm.springframework.web.struts.DispatchActionSupport;
import com.hollycrm.unicom.sheet.BeanResource;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.business.BusinessSupportManager;
import com.hollycrm.unicom.sheet.lightweight.IAcceptRecord;
import java.util.ArrayList;
import java.util.Iterator;
import com.hollycrm.unicom.sheet.domain.AcceptRecord;
import com.hollycrm.unicom.sheet.domain.MainSheet;
/**
 * ListDealSheetAction
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Tian Kai
 * @version 1.0
 */
public class QueryAcceptRecordAction
    extends DispatchActionSupport {
    /**
     * execDefault
     *
     * @param mapping ActionMapping
     * @param actionForm ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws Exception
     * @return ActionForward
     */
    public ActionForward execDefault(ActionMapping mapping,
                                     ActionForm actionForm,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws
        Exception {
        AcceptRecordForm form = (AcceptRecordForm)actionForm;
        String mainSheetFlowNO = request.getParameter("sheetFlowNO");
        String archive = request.getParameter("archive");
        BusinessSupportManager bsm = getBusinessSupportManager(archive);
        String lastAcceptRecordID = request.getParameter("lastAcceptRecordID");

        if(lastAcceptRecordID == null){
            MainSheet mainSheet = (MainSheet) bsm.retrieveMainSheet(mainSheetFlowNO);
            lastAcceptRecordID = mainSheet.getLastAcceptRecordID();
        }

        List acceptRecords = bsm.listAcceptRecords(mainSheetFlowNO);

        List appendRecords = new ArrayList();
        if(acceptRecords!=null){
            for (Iterator iter = acceptRecords.iterator(); iter.hasNext(); ) {
                AcceptRecord item = (AcceptRecord) iter.next();
                if (!item.getRecordID().equals(lastAcceptRecordID))
                    appendRecords.add(item);
            }
        }
        request.setAttribute("mobileTeleNO",form.getMobileTeleNO());
        request.setAttribute("acceptRecords", appendRecords);
        return mapping.findForward("success");
    }

    public ActionForward execView(ActionMapping mapping,
                                     ActionForm actionForm,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws
        Exception {
        String acceptRecordId = request.getParameter("acceptRecordId");
        String archive = request.getParameter("archive");
        String mobileTeleNO = request.getParameter("mobileTeleNO");

        BusinessSupportManager bsm = getBusinessSupportManager(archive);
        IAcceptRecord acceptRecordInfo = (IAcceptRecord)bsm.retriveAcceptRecord(acceptRecordId);
        request.setAttribute("acceptRecordInfo", acceptRecordInfo);
        return mapping.findForward("success");
    }

    /**
     * getBusinessSupportManager
     *
     * @param archive MainSheetForm
     * @return BusinessSupportManager
     */
    private BusinessSupportManager getBusinessSupportManager(String archive) {
        if (Constants.TRUE.equals(archive))
            return (BusinessSupportManager) getBean(BeanResource.
                ARCHIVEBUSINESSSUPPORT_MANAGER);
        else
            return (BusinessSupportManager) getBean(BeanResource.
                BUSINESSSUPPORT_MANAGER);
    }

    /**
     *
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
//    public ActionForward execRelateRecord(ActionMapping mapping,
//                                     ActionForm actionForm,
//                                     HttpServletRequest request,
//                                     HttpServletResponse response) throws
//        Exception {
//        AcceptForm form = (AcceptForm)actionForm;
//        String mainSheetFlowNO = form.getAcceptRecord().getMainSheetFlowNO();
//        String recordID = form.getAcceptRecord().getRecordID();
//        BusinessSupportManager bsm = (BusinessSupportManager)
//            getBean(BeanResource.BUSINESSSUPPORT_MANAGER);
//        List acceptRecords = bsm.listRelateAcceptRecords(mainSheetFlowNO,recordID);
//        request.setAttribute("acceptRecords", acceptRecords);
//        return mapping.findForward("success");
//    }


}
